package com.css.fxfzysx.modules.vabStructure.controller;

import com.css.fxfzysx.base.response.RestResponse;
import com.css.fxfzysx.modules.cztDataMange.entity.CbvInfoEntity;
import com.css.fxfzysx.modules.vabStructure.param.CustomVulnerabilityParam;
import com.css.fxfzysx.modules.vabStructure.param.EstimateParam;
import com.css.fxfzysx.modules.vabStructure.param.MatchingParam;
import com.css.fxfzysx.modules.vabStructure.service.CustomVulnerabilityService;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 自定义易损性
 */
@RestController
@Slf4j
@RequestMapping("customVulnerability")
public class CustomVulnerabilityController {

    @Autowired
    private CustomVulnerabilityService customVulnerabilityService;

    /**
     * 数据匹配
     * @return
     */
    @PostMapping("dataMatching")
    public RestResponse dataMatching(@RequestBody MatchingParam param){
        return customVulnerabilityService.dataMatching(param);
    }

    /**
     * 人工导入分页查询
     */
    @GetMapping("queryData")
    public RestResponse queryData(CbvInfoEntity cbvInfoEntity, HttpServletRequest request){
        return customVulnerabilityService.queryData(cbvInfoEntity,request);
    }

    /**
     * 移入（移出）基础库
     * @return
     */
    @GetMapping("moveBasicLibrary")
    public RestResponse moveBasicLibrary(CustomVulnerabilityParam param){
        return customVulnerabilityService.moveBasicLibrary(param);
    }

    /**
     * 生成矩阵或者曲线
     * @return
     */
    @GetMapping("generateMatrix")
    public RestResponse generateMatrix(String cbviId,String province){
        return customVulnerabilityService.generateMatrix(cbviId,province);
    }

    /**
     *人工导入页面导出excel
     */
    @GetMapping("exportData")
    public void exportData(HttpServletResponse response, String cbviId){
        customVulnerabilityService.exportData(response,cbviId);
    }

    /**
     *曲线和矩阵互相转换
     */
    @GetMapping("curveMatrixTransformation")
    public RestResponse curveMatrixTransformation(String id,String province){
        return customVulnerabilityService.curveMatrixTransformation(id,province);
    }

    @PostMapping("getByRegion")
    public RestResponse getByRegion(@RequestBody HashMap<String,Object> map){
        try {
            List<EstimateParam> list = null;
            try {
                ObjectMapper mapper = new ObjectMapper();
                list = mapper.convertValue(map.get("list"),new TypeReference<List<EstimateParam>>() { });
            } catch (Exception e) {
                list = new ArrayList<>();
            }
            String fortification = map.get("fortification").toString();
            return customVulnerabilityService.getByRegion(list,fortification);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return RestResponse.fail("查询失败!");
        }
    }


}
